import isEmpty from 'lodash/isEmpty';
import {
  message
} from 'antd';
import {
  WeaLocaleProvider
} from 'ecCom';
import loadjs from 'loadjs';
const getLabel = WeaLocaleProvider.getLabel;
/**
  正常
  迟到
  早退
  旷工
  漏签
  请假
  出差
  公出
  加班
  异常数据抵扣
 */
// NORMAL,BELATE,LEAVEEARLY,ABSENT,NOSIGN,LEAVE,EVECTION,OUTDAYS,OVERTIME,OTHER;

export const getIcon = type => {
  let res = {};
  if (type == 'LEAVE') {
    res.color = '#2DB7F5';
    res.icon = 'icon-Human-resources-leave';
  }
  if (type == 'EVECTION') {
    res.color = '#48C651';
    res.icon = 'icon-Human-resources-business-travel';
  }
  if (type == 'OUTDAYS') {
    res.color = '#4F8CFF';
    res.icon = 'icon-Human-resources-work';
  }
  if (type == 'OVERTIME') {
    res.color = '#8D6BF3';
    res.icon = 'icon-Human-resources-overtime';
  }
  if (type == 'OTHER') {
    res.color = '#ABB4C1';
    res.icon = 'icon-Human-resources-abnormal-small2';
  }
  return res;
}

export const getIconColor = type => {
  let res = {};
  if (type == 'ALL') {
    res.color = '#2DB7F5';
    res.name = getLabel(82857, '全部');
    res.icon = 'icon-Human-resources-all';
  }
  if (type == 'NORMAL') {
    res.color = '#2DB7F5';
    res.name = getLabel('225', "正常");
    res.icon = 'icon-Human-resources-normal';
  }
  if (type == 'BELATE') {
    res.color = '#FF8F00';
    res.name = getLabel('20081', "迟到");
    res.bcolor = '#FFF6EB';
    res.icon = 'icon-Human-resources-late';
  }
  if (type == 'LEAVEEARLY') {
    res.color = '#7326FF';
    res.name = getLabel('20082', "早退");
    res.bcolor = '#F1E9FF';
    res.icon = 'icon-Human-resources-Leave-early';
  }
  if (type == 'ABSENT') {
    res.color = '#FC4B6C';
    res.name = getLabel('20085', "旷工");
    res.bcolor = '#FFE6EB';
    res.icon = 'icon-Human-resources-Absenteeism';
  }
  if (type == 'NOSIGN') {
    res.color = '#BD10E0';
    res.name = getLabel('20086', "漏签");
    res.bcolor = '#FAECFD';
    res.icon = 'icon-Human-resources-check';
  }
  if (type == 'LEAVE') {
    res.color = '#2DB7F5';
    res.name = getLabel('670', "请假");
    res.bcolor = '#EFFAFF';
    res.icon = 'icon-Human-resources-leave';
  }
  if (type == 'EVECTION') {
    res.color = '#30BE3A';
    res.name = getLabel('20084', "出差");
    res.bcolor = '#EFFAF0';
    res.icon = 'icon-Human-resources-business-travel';
  }
  if (type == 'OUTDAYS') {
    res.color = '#4F8CFF';
    res.name = getLabel('24058', "公出");
    res.bcolor = '#DFEBFF';
    res.icon = 'icon-Human-resources-work';
  }
  if (type == 'OVERTIME') {
    res.color = '#8D6BF3';
    res.name = getLabel('6151', "加班");
    res.bcolor = '#FAECFD';
    res.icon = 'icon-Human-resources-overtime-small';
  }
  if (type == 'OTHER') {
    res.color = '#ABB4C1';
    res.name = getLabel('32104', "异常");
    res.title = getLabel('132031', "异常数据抵扣");
    res.bcolor = '#F0F6FF';
    res.icon = 'icon-Human-resources-abnormal-small2';
  }
  return res;
}

export const validate = (conditions, params = {}) => {
  if (!isEmpty(conditions)) {
    for (let i = 0, leg = conditions.length; i < leg; i++) {
      let cd = conditions[i];
      if (cd.hide) continue;
      for (let j = 0, length = cd.items.length; j < length; j++) {
        let c = cd.items[j];
        if (c) {
          if (c.otherParams && c.otherParams.hide) continue;
          let domkey = c.domkey[0];
          let viewAttr = c.viewAttr;
          if (c.conditionType == 'BROWSER') viewAttr = c.browserConditionParam.viewAttr;
          if (viewAttr == '3' && !params[domkey]) {
            if (domkey == 'managerid' || domkey == 'belongto') { //直接上级 主账号不用校验
            } else {
              //message.warning(domkey+'必要信息不完整！');
              // message.warning(getLabel('15859', "必要信息不完整！"));
              message.warning(`"${c.label}"${getLabel(508517, "未填写")}`);
              return false;
            }
          }
        }
      }
    }
  }
  return true;
}

export const getFormParamValue = (conditions = {}, fieldname) => {
  let fieldvalue = '';
  if (!isEmpty(conditions)) {
    for (let i = 0, leg = conditions.length; i < leg; i++) {
      let cd = conditions[i];
      for (let j = 0, length = cd.items.length; j < length; j++) {
        let c = cd.items[j];
        if (c) {
          let domkey = c.domkey[0];
          if (domkey == fieldname) {
            fieldvalue = c.value;
          }
        }
      }
    }
  }
  return fieldvalue;
}

export const createQRCode = data => {
  //生成二维码
  var txt = "BEGIN:VCARD \n" +
    "VERSION:3.0 \n" +
    "N:" + data.lastname + " \n" +
    "TEL;CELL;VOICE:" + data.mobile + "\n" +
    "TEL;WORK;VOICE:" + data.telephone + "\n" +
    "EMAIL:" + data.email + " \n" +
    "TITLE:" + data.jobtitle + " \n" +
    "ROLE:" + data.department + "\n" +
    "ADR;WORK:" + data.locationname + " \n" +
    "END:VCARD";
  return utf16to8(txt);

  function utf16to8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for (i = 0; i < len; i++) {
      c = str.charCodeAt(i);
      if ((c >= 0x0001) && (c <= 0x007F)) {
        out += str.charAt(i);
      } else if (c > 0x07FF) {
        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
        out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
      } else {
        out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
        out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
      }
    }
    return out;
  }
}

// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
export const DateFormat = (date, fmt) => {
  if (date && fmt) {
    let _this = date
    const type = Object.prototype.toString.call(date)
    if (type == "[object String]") {
      let dateArray = date.split(" "),
        d1 = dateArray[0].split("-"),
        d2 = dateArray[1] ? dateArray[1].split(":") : [];
      _this = new Date((d1[0] || 0) * 1, (d1[1] || 0) * 1 - 1, (d1[2] || 0) * 1, (d2[0] || 0) * 1, (d2[1] || 0) * 1, (d2[2] || 0) * 1)
    } else if (type == "[object Date]") {

    } else {
      return ""
    }
    let o = {
      "M+": _this.getMonth() + 1, //月份
      "d+": _this.getDate(), //日
      "h+": _this.getHours() % 12 == 0 ? 12 : _this.getHours() % 12, //小时
      "H+": _this.getHours(), //小时
      "m+": _this.getMinutes(), //分
      "s+": _this.getSeconds(), //秒
      "q+": Math.floor((_this.getMonth() + 3) / 3), //季度
      "S": _this.getMilliseconds() //毫秒
    };
    let week = {
      "0": "\\u65e5",
      "1": "\\u4e00",
      "2": "\\u4e8c",
      "3": "\\u4e09",
      "4": "\\u56db",
      "5": "\\u4e94",
      "6": "\\u516d"
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (_this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
      let weekStr = ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\\u661f\\u671f" : "\\u5468") : "") + week[_this.getDay() + ""]
      fmt = fmt.replace(RegExp.$1, Utils.GB2312UnicodeConverter.ToGB2312(weekStr));
    }
    for (let k in o) {
      if (new RegExp("(" + k + ")").test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      }
    }
    return fmt;
  } else {
    return ""
  }
}

export const calFormHeight = (groupLength, children) => {
  let childrenLength = 0;
  children.map(c => {
    if (c.colSpan != null) {
      childrenLength += c.colSpan == 2 ? 1 : 2;
    } else
      childrenLength += 1;
  })
  let height = (childrenLength / 2 + childrenLength % 2) * 52 + 10;
  if (groupLength > 1)
    height += groupLength * 45;
  if (height > 300)
    return 300;
  return height;
}

export const convertCalendarDate = (date) => {
  const y = date.getYear();
  let d = date.getDayOfMonth();
  if (d < 10)
    d = '0' + d;
  let m = date.getMonth() + 1;
  if (m < 10)
    m = '0' + m;
  return `${y}-${m}-${d}`;
}

export const GetUrlParam = (url, paraName) => {
  let arrObj = url.split("?");
  if (arrObj.length > 1) {
    let arrPara = arrObj[1].split("&");
    let arr;
    for (let i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split("=");
      if (arr !== null && arr[0] === paraName) {
        return arr[1];
      }
    }
    return "";
  } else {
    return "";
  }
}

export const carousel = (pic, index, parent) => {
  const imgPool = [];
  let indexNum = 0;

  let clickImgUrl = pic;
  let clickImgId = GetUrlParam(clickImgUrl, "fileid");

  const dom = `.${parent}:eq(${index}) img`;

  $(dom).each((index, img) => {
    imgPool.push(img.currentSrc ? img.currentSrc : img.src);
  });

  for (let i = 0; i < imgPool.length; i++) {
    let imgid = GetUrlParam(imgPool[i], "fileid");
    if (clickImgId === imgid) {
      break;
    } else {
      indexNum++;
    }
  }

  let isShowPicture = true;
  if (clickImgId === "") {
    isShowPicture = false;
  }

  if (isShowPicture) {
    if (window.IMCarousel) {
      window.IMCarousel.showImgScanner4Pool(true, imgPool, indexNum, null, window.top);
    } else {
      loadjs(["/social/js/drageasy/drageasy.js", "/social/js/bootstrap/js/bootstrap.js?v=20171218", "/social/im/js/IMUtil_wev8.js", "/social/js/imcarousel/imcarousel.js"], () => {
        window.IMCarousel.showImgScanner4Pool(true, imgPool, indexNum, null, window.top);
      });
    }
  }
}